/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-28
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-13
 * @Description: 联系人本地储存
 */
import 'package:chat_client/enums/contact_enum.dart';
import 'package:hive/hive.dart';

import 'package:chat_client/models/apply_model.dart';
import 'package:chat_client/models/contact_model.dart';

import 'package:chat_client/repositories/hive_repository.dart';

/// 联系人本地储存
class ContactRepository {
  // 单例
  ContactRepository._internal() {}
  static final ContactRepository _instance = ContactRepository._internal();
  static ContactRepository get instance => _instance;

  // 打开数据库
  final _hive = HiveService.instance;
  late final Box<Contact> _contactBox;
  late final Box<Apply> _applicationBox;

  Future<void> init() async {
    _contactBox = await _hive.openBox<Contact>('contact');
    _applicationBox = await _hive.openBox<Apply>('apply');
  }

  /// 申请部分
  // 储存多个好友申请
  Future<void> setApplyList(List<Apply> applyList) async {
    // 将每个 Apply 单独存储在不同的键下
    for (var apply in applyList) {
      await _applicationBox.put(apply.opponentId, apply);
    }
  }

  // 储存单个好友申请
  Future<void> setApply(Apply apply) async {
    await _applicationBox.put(apply.opponentId, apply);
  }

  // 获取本地所有Apply
  List<Apply> getAllApplyList() {
    // 获取所有存储的键
    return _applicationBox.values.toList();
  }

  // 获取单个 Apply
  Apply? getApply(String key) {
    return _applicationBox.get(key);
  }

  // 修改单个 Apply的处理状态
  Future<Apply> updateApply(
      {required String opponentId, required int status}) async {
    // 获取当前存储的 Apply
    Apply? currentApply = _applicationBox.get(opponentId);

    if (currentApply != null) {
      // 更新status字段
      currentApply.status = status;
      // 将更新后的 Apply 存回 Hive
      await _applicationBox.put(opponentId, currentApply);

      return currentApply; // 返回更新后的 Apply 实例
    } else {
      // 如果没有找到对应的 Apply，返回原来的 Apply 实例或报错
      throw Exception('Apply with key $opponentId not found');
    }
  }

  /// 联系人部分
  // 储存联系人列表数据
  Future<void> setContactgetContactList(List<Contact> contactList) async {
    for (var contact in contactList) {
      await _contactBox.put(contact.userId, contact);
    }
  }

  // 获取本地联系人列表数据
  List<Contact> getContactList() {
    final List<Contact> list = _contactBox.values.toList();
    return list
        .where((contact) => contact.status == ContactStatus.friend.value)
        .toList();
  }

  // 获取本地黑名单列表数据
  List<Contact> getBlockList() {
    final List<Contact> list = _contactBox.values.toList();
    final filterList = list
        .where((contact) => contact.status == ContactStatus.black.value)
        .toList();
    return filterList;
  }

  // 储存单个联系人数据
  Future<void> setContact(Contact contact) async {
    await _contactBox.put(contact.userId, contact);
  }

  // 获取单个联系人数据
  Contact? getContact(String userId) {
    return _contactBox.get(userId);
  }

  // 修改本地单个联系人数据
  Future<void> updateContact(Contact contact) async {
    await _contactBox.put(contact.userId, contact);
  }

  // 删除本地联系人数据
  Future<void> deleteContact(String userId) async {
    await _contactBox.delete(userId);
  }
}
